/*
  This file is a part of hashutils package.
  (c) 2004 Zeljko Vrba <zvrba@globalnet.hr>

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef CDT_BBLOCK_H__
#define CDT_BBLOCK_H__

#include <stddef.h>
#include <cdt.h>
#include <sfio.h>
#include <vmalloc.h>

struct bblock_offset {
	unsigned int off;			/* block offset in file */
	struct bblock_offset *next;	/* next in list */
};

/** Binary block structure, CDT and vmalloc discipline. */
struct bblock_head {
	Dtlink_t _l;				/* link */
	struct bblock_offset *offlist, *tail; /* list of offsets and list tail */
	char h[1];					/* hash */
};

struct bblock_iterator {
	char * (*next)(struct bblock_iterator*); /* get next block */
	int halg;					/* hash algorithm */
	unsigned current, end, len;	/* current/end offset and block length */
	char *data;					/* pointer to data */
	char *h;					/* pointer to the hash of data */
};

struct bblock_file_iterator {
	struct bblock_iterator it;
	Sfio_t *f;					/* file */
	char h[1];					/* hash buffer */
};

struct bblock_mem_iterator {
	struct bblock_iterator it;
	char h[1];					/* hash buffer */
};

/*
  bblock_head discipline for containers. Note that bblock must ALWAYS
  be allocated before inserted in the container. The container make
  and free functions are always set to NULL.
*/
extern Dtdisc_t Cdt_BBlockDiscipline;

/*
  Ugly, but portable way to calculate minimum allocation size given
  the hash length in bytes (we have to obey struct member alignment).
  h begins at some offset; adding hlen to that offset gives the necessary
  size of struct.
*/
#define BBLOCKHSZ(hlen) (offsetof(struct bblock_head, h) + hlen)

struct bblock_iterator *BBlock_file_begin(Sfio_t *f, unsigned s, unsigned e, unsigned l, int halg);
struct bblock_iterator *BBlock_mem_begin(char *m, unsigned s, unsigned e, unsigned l, int halg);
void BBlock_insert(Dt_t *set, char *h, unsigned off);

/**
   Advance the iterator to the next block. Returns the hash value or NULL if
   the iterator reached the end.
*/
#define BBLOCK_NEXT(bi) (((struct bblock_iterator*)bi)->next((struct bblock_iterator*)bi))

extern Vmalloc_t *BBlock_vmpool;
extern Vmdisc_t *Vmdcmmap;

#endif /* CDT_BBLOCK_H__ */
